# Contributor: Milan P. Stanić <mps@arvanta.net>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=keydb
pkgver=6.3.4
pkgrel=0
pkgdesc="High performance multithreaded fork of Redis"
url="https://keydb.dev"
# armhf,armv7,x86: not supported
# ppc64le: fails to build (undefined reference to 'getcontext')
arch="all !armhf !armv7 !ppc64le !x86"
# hiredis: BSD-3-Clause
# hdrhistogram, jemalloc, linenoise: BSD-2-Clause
# lua: MIT
license="BSD-3-Clause AND BSD-2-Clause AND MIT"
makedepends="
	libunwind-dev
	linux-headers
	openssl-dev
	util-linux-dev
	zlib-dev
	"
checkdepends="
	procps
	tcl
	"
pkgusers="keydb"
pkggroups="keydb"
install="$pkgname.pre-install $pkgname.post-install"
subpackages="
	$pkgname-benchmark
	$pkgname-cli
	$pkgname-openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/JohnSully/KeyDB/archive/v$pkgver.tar.gz
	keydb.conf.patch
	sentinel.conf.patch
	deps-makefile.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname-sentinel.initd
	$pkgname-sentinel.confd
	"
builddir="$srcdir/KeyDB-$pkgver"
options="!check"  # tests fail randomly

# USE_JEMALLOC uses bundled jemalloc
_make_opts="\
	BUILD_TLS=yes \
	NO_MOTD=yes \
	PREFIX=/usr \
	USE_JEMALLOC=yes \
	USE_SYSTEMD=no \
	"

prepare() {
	default_prepare
	update_config_guess
}

build() {
	export CFLAGS="$CFLAGS -O2"

	make $_make_opts all
}

check() {
	make test $_make_opts
}

package() {
	make install $_make_opts INSTALL_BIN="$pkgdir/usr/bin"

	cd "$pkgdir"

	# NOTE: /etc/keydb and /etc/keydb/sentinel.conf must be writable for keydb,
	# otherwise Sentinel fails to start.
	install -d -m 750 -o keydb -g keydb \
		etc/keydb \
		etc/keydb/keydb.d \
		var/lib/keydb \
		var/lib/keydb/sentinel

	install -D -m 640 -o root -g keydb "$builddir"/keydb.conf etc/keydb/keydb.conf
	install -D -m 640 -o keydb -g keydb "$builddir"/sentinel.conf etc/keydb/sentinel.conf

	install -D -m 755 "$srcdir"/keydb.initd etc/init.d/keydb
	install -D -m 755 "$srcdir"/keydb-sentinel.initd etc/init.d/keydb-sentinel
	install -D -m 644 "$srcdir"/keydb.confd etc/conf.d/keydb
	install -D -m 644 "$srcdir"/keydb-sentinel.confd etc/conf.d/keydb-sentinel

	install -d -m 750 -o keydb -g keydb \
		var/lib/keydb \
		var/lib/keydb/sentinel
}

benchmark() {
	pkgdesc="KeyDB benchmarking tool"

	amove usr/bin/keydb-benchmark
}

cli() {
	pkgdesc="KeyDB CLI client"

	amove usr/bin/keydb-cli
}

sha512sums="
6ac5060dc60df878668830336821cac740690be5525bc8c4a31a522c629b73df2da49f6d53fe8ebfec171efef258c9253d91243fe57a470aaabeddea2ac5247b  keydb-6.3.4.tar.gz
add4b21b9ecc8c18046462caaac58cc26c837136573c912b0904e107ce2ae684a409e575a924b33310955b9ac76b42330b494efcbe94e02ede5136244e7b165a  keydb.conf.patch
50ce23119c1ddb6e63999740299711bd3a7a8500ce6b75f7850012b2edae13051dba43e7628788255f2d16dfe913da92222cae9908a3d8f7b619c2ee70e2a44f  sentinel.conf.patch
be06084939bb8d26f638f09b21c1edcc06d738b5cd0647ab2bc3f4b01f5a61c63486e1ac913de30278a7d9474cf2fc965903b48305551aa699ec43834acaf56a  deps-makefile.patch
b70d0bc0207c606be8ef3d099769075c8925c989fe2ca121e4368407fb1662010e18a745c1602679a376dd97eaf2e1ee01c1d41e7ab02e2019172b6b4adb2e6a  keydb.initd
91dfd649c326c3bf0be24094e662f7347d84028527ddc6e6819564ad862deadd7b246e0f518b38a3c4e768c1ef1c54471ddb8f56c8c54177c61e15dff0d7ba60  keydb.confd
8c7825e2c6a579cca3d4197be4811bfb37d516cd5d31c667ffc589245306f4a8e8ec86eeab170d3e90d2f069c691dd779236c97848794a483206df09134e5d65  keydb-sentinel.initd
d86345a469b5ae731b7dc80577b1f71ac97928cba162f7376a9088ada52abe99c9e042ff5b827c40cab0267b95967e5ffd8fb9a23c0d55822aba22e0ab4f2086  keydb-sentinel.confd
"
